<?php

namespace app\communal\controller;

use app\model\Question as ModelQuestion;
use app\model\QuestionClass as ModelQuestionClass;
use app\logic\Page AS LogicPage;

class QuestionForClient extends Question
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 获取问题列表 \n
     * URI : /communal/client.question.list
     * @param :
     *  name        |  type  | null | description
     * -------------|--------|------|-------------
     *  moduleName  | string | 必填  | 模块名称 user-用户端、courier-快递、driver-司机端
     *  classId     | int    | 必填  | 分类ID
     *
     * @return
     *  name      |  type  | description
     * -----------|--------|-------------
     *  totalPage | string | 总页数
     *  list      | array  | 列表
     *
     * list:
     *  name       | type   | description
     * ------------|--------|-------------
     *  questionId | int    | 问题ID
     *  title      | string | 问题标题
     *
     */
    public function questionList()
    {
        $str_module_name = $this->_getContentParams('moduleName');
        _toCompare(in_array($str_module_name, $this->arr_module_name_key), '===', false, 19030); // 模块名称不正确

        $int_class_id = $this->_getContentParams('classId');
        $bool_result = ModelQuestionClass::getInstance()->checkId($int_class_id, $str_module_name);
        _toCompare($bool_result, '==', false, 13003);

        $arr_where = [
            'client' => $str_module_name,
            'class_id' => $int_class_id,
            'is_show' => 1,
        ];

        $int_now_page = $this->_getContentParams('page');
        $int_list_rows = $this->_getContentParams('listRows');;
        $str_field = 'id AS questionId,title';
        $obj = ModelQuestion::getInstance();
        $arr_list = LogicPage::getInstance()->getList($obj, $arr_where, $str_field, $int_now_page, $int_list_rows, 'sort ASC');
        _returnData($arr_list);
    }


    /**
     * 获取问题详情 \n
     * URI : /communal/client.question.info
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function questionInfo()
    {
        $str_module_name = $this->_getContentParams('moduleName');
        _toCompare(in_array($str_module_name, $this->arr_module_name_key), '===', false, 19030); // 模块名称不正确

        $int_question_id = $this->_getContentParams('questionId');
        $bool_result = ModelQuestion::getInstance()->checkId($int_question_id, $str_module_name);
        _toCompare($bool_result, '==', false, 13002);
        $arr_info = (array)ModelQuestion::getInstance()->toFind(['id' => $int_question_id], 'title,content');
        _returnData($arr_info);
    }
}
